﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AccesoDatos
{
    class Password
    {
        public static string EncriptarContraseña(string contraseña, string ID)
        {
            string encriptada = "";
            int key = Key(ID);

            for (int i = 0; i < contraseña.Length; i++)
            {
                encriptada += (Int32.Parse(Encriptar(contraseña[i])) + key).ToString();
                encriptada += " ";
            }
            return encriptada;
        }

        public static string DesencriptarContraseña(string encriptada, string ID)
        {
            string contraseña = "";
            int key = Key(ID);
            string numero = "";

            for (int i = 0; i < encriptada.Length; i++)
            {
                if (!encriptada[i].Equals(' '))
                {
                    numero += encriptada[i];
                }
                else
                {
                    contraseña += Desencriptar(Int32.Parse(numero) - key);
                    numero = "";
                }
            }
            return contraseña;
        }

        private static string Encriptar(char caracter)
        {
            string res = "";
            try
            {
                int num = Int32.Parse(caracter.ToString());
                switch (num)
                {
                    case 1:
                        res = "55";
                        break;
                    case 2:
                        res = "56";
                        break;
                    case 3:
                        res = "57";
                        break;
                    case 4:
                        res = "58";
                        break;
                    case 5:
                        res = "59";
                        break;
                    case 6:
                        res = "60";
                        break;
                    case 7:
                        res = "61";
                        break;
                    case 8:
                        res = "62";
                        break;
                    case 9:
                        res = "63";
                        break;
                    case 0:
                        res = "64";
                        break;
                }
                return res;
            }
            catch (FormatException)
            {
                switch (caracter)
                {
                    case 'A':
                        res = "1";
                        break;
                    case 'B':
                        res = "2";
                        break;
                    case 'C':
                        res = "3";
                        break;
                    case 'D':
                        res = "4";
                        break;
                    case 'E':
                        res = "5";
                        break;
                    case 'F':
                        res = "6";
                        break;
                    case 'G':
                        res = "7";
                        break;
                    case 'H':
                        res = "8";
                        break;
                    case 'I':
                        res = "9";
                        break;
                    case 'J':
                        res = "10";
                        break;
                    case 'K':
                        res = "11";
                        break;
                    case 'L':
                        res = "12";
                        break;
                    case 'M':
                        res = "13";
                        break;
                    case 'N':
                        res = "14";
                        break;
                    case 'Ñ':
                        res = "15";
                        break;
                    case 'O':
                        res = "16";
                        break;
                    case 'P':
                        res = "17";
                        break;
                    case 'Q':
                        res = "18";
                        break;
                    case 'R':
                        res = "19";
                        break;
                    case 'S':
                        res = "20";
                        break;
                    case 'T':
                        res = "21";
                        break;
                    case 'U':
                        res = "22";
                        break;
                    case 'V':
                        res = "23";
                        break;
                    case 'W':
                        res = "24";
                        break;
                    case 'X':
                        res = "25";
                        break;
                    case 'Y':
                        res = "26";
                        break;
                    case 'Z':
                        res = "27";
                        break;
                    case 'a':
                        res = "28";
                        break;
                    case 'b':
                        res = "29";
                        break;
                    case 'c':
                        res = "30";
                        break;
                    case 'd':
                        res = "31";
                        break;
                    case 'e':
                        res = "32";
                        break;
                    case 'f':
                        res = "33";
                        break;
                    case 'g':
                        res = "34";
                        break;
                    case 'h':
                        res = "35";
                        break;
                    case 'i':
                        res = "36";
                        break;
                    case 'j':
                        res = "37";
                        break;
                    case 'k':
                        res = "38";
                        break;
                    case 'l':
                        res = "39";
                        break;
                    case 'm':
                        res = "40";
                        break;
                    case 'n':
                        res = "41";
                        break;
                    case 'ñ':
                        res = "42";
                        break;
                    case 'o':
                        res = "43";
                        break;
                    case 'p':
                        res = "44";
                        break;
                    case 'q':
                        res = "45";
                        break;
                    case 'r':
                        res = "46";
                        break;
                    case 's':
                        res = "47";
                        break;
                    case 't':
                        res = "48";
                        break;
                    case 'u':
                        res = "49";
                        break;
                    case 'v':
                        res = "50";
                        break;
                    case 'w':
                        res = "51";
                        break;
                    case 'x':
                        res = "52";
                        break;
                    case 'y':
                        res = "53";
                        break;
                    case 'z':
                        res = "54";
                        break;
                }
                return res;
            }
        }

        private static string Desencriptar(int numero)
        {
            string res = "";
            switch (numero)
            {
                case 1:
                    res = "A";
                    break;
                case 2:
                    res = "B";
                    break;
                case 3:
                    res = "C";
                    break;
                case 4:
                    res = "D";
                    break;
                case 5:
                    res = "E";
                    break;
                case 6:
                    res = "F";
                    break;
                case 7:
                    res = "G";
                    break;
                case 8:
                    res = "H";
                    break;
                case 9:
                    res = "I";
                    break;
                case 10:
                    res = "J";
                    break;
                case 11:
                    res = "K";
                    break;
                case 12:
                    res = "L";
                    break;
                case 13:
                    res = "M";
                    break;
                case 14:
                    res = "N";
                    break;
                case 15:
                    res = "Ñ";
                    break;
                case 16:
                    res = "O";
                    break;
                case 17:
                    res = "P";
                    break;
                case 18:
                    res = "Q";
                    break;
                case 19:
                    res = "R";
                    break;
                case 20:
                    res = "S";
                    break;
                case 21:
                    res = "T";
                    break;
                case 22:
                    res = "U";
                    break;
                case 23:
                    res = "V";
                    break;
                case 24:
                    res = "W";
                    break;
                case 25:
                    res = "X";
                    break;
                case 26:
                    res = "Y";
                    break;
                case 27:
                    res = "Z";
                    break;
                case 28:
                    res = "a";
                    break;
                case 29:
                    res = "b";
                    break;
                case 30:
                    res = "c";
                    break;
                case 31:
                    res = "d";
                    break;
                case 32:
                    res = "e";
                    break;
                case 33:
                    res = "f";
                    break;
                case 34:
                    res = "g";
                    break;
                case 35:
                    res = "h";
                    break;
                case 36:
                    res = "i";
                    break;
                case 37:
                    res = "j";
                    break;
                case 38:
                    res = "k";
                    break;
                case 39:
                    res = "l";
                    break;
                case 40:
                    res = "m";
                    break;
                case 41:
                    res = "n";
                    break;
                case 42:
                    res = "ñ";
                    break;
                case 43:
                    res = "o";
                    break;
                case 44:
                    res = "p";
                    break;
                case 45:
                    res = "q";
                    break;
                case 46:
                    res = "r";
                    break;
                case 47:
                    res = "s";
                    break;
                case 48:
                    res = "t";
                    break;
                case 49:
                    res = "u";
                    break;
                case 50:
                    res = "v";
                    break;
                case 51:
                    res = "w";
                    break;
                case 52:
                    res = "x";
                    break;
                case 53:
                    res = "y";
                    break;
                case 54:
                    res = "z";
                    break;
                case 55:
                    res = "1";
                    break;
                case 56:
                    res = "2";
                    break;
                case 57:
                    res = "3";
                    break;
                case 58:
                    res = "4";
                    break;
                case 59:
                    res = "5";
                    break;
                case 60:
                    res = "6";
                    break;
                case 61:
                    res = "7";
                    break;
                case 62:
                    res = "8";
                    break;
                case 63:
                    res = "9";
                    break;
                case 64:
                    res = "0";
                    break;
            }
            return res;
        }

        private static int Key(string ID)
        {
            int key = 0;
            for (int i = 0; i < ID.Length; i++)
            {
                key += Int32.Parse(Encriptar(ID[i]));
            }
            return key;
        }
    }
}
